﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Xml;

namespace DusonFramework.ContactService.Email {
    /// <summary>
    /// Get Contract List of 126.com 
    /// </summary>
    public class Email126Service : IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "126.com"; }
        }
        public string StartUrl {
            get { return "https://entry.mail.126.com/cgi/login?redirTempName=https.htm&hid=10010102&lightweight=1&verifycookie=1&language=0&style=-1"; }
        }
        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }
        #endregion

        #region Events

        #endregion

        #region Methods
        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            string returnUrl = StartUrl;
            CookieContainer cc = new CookieContainer();
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("bCookie", "");
            nvc.Add("domain", "126.com");
            nvc.Add("enter.x", "μ? ?");
            nvc.Add("language", "0");
            nvc.Add("pass", pwd);
            nvc.Add("remUser", "");
            nvc.Add("secure", "");
            nvc.Add("style", "-1");
            nvc.Add("user", userName);
            string postContent = WebOpHelper.GetNameValueRowData(nvc, '&');
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://mail.126.com/";
            string contentType = "application/x-www-form-urlencoded";
            string startPageHTMl = WebOpHelper.Post(StartUrl, accept, contentType, refer,
                language, postContent, cc, Encoding.GetEncoding("gb2312"), out returnUrl);
            string loginUrl = WebOpHelper.Resove(startPageHTMl, "window.location.replace(\"", "\")");
            Uri u = new Uri(loginUrl);
            string sid = WebOpHelper.GetQ(u, "sid");
            loginUrl = "http://" + u.Host + "/a/s" + u.Query + "&func=global:sequential";
            NameValueCollection cookies = new NameValueCollection();
            cookies.Add("logType", "logType");
            cookies.Add("NETEASE_SSN", userName);
            cookies.Add("ntes_mail_firstpage", "normal");
            cookies.Add("nts_mail_user", userName);
            cookies.Add("mail_host", u.Host);
            cookies.Add("mail_sid", sid);
            cookies.Add("mail_uid", userName + "@126.com");
            cookies.Add("dm3", "dm3");
            string cookieRow = WebOpHelper.GetNameValueRowData(cookies, ',');
            u = new Uri(loginUrl);
            cc.SetCookies(u, cookieRow);
            postContent = "<?xml version=\"1.0\"?><object><array name=\"items\"><object><string name=\"func\">pab:searchContacts</string><object name=\"var\"><array name=\"order\"><object><string name=\"field\">FN</string><boolean name=\"ignoreCase\">true</boolean></object></array></object></object><object><string name=\"func\">user:getSignatures</string></object><object><string name=\"func\">pab:getAllGroups</string></object></array></object>";
            contentType = "application/xml";
            string html = WebOpHelper.Post(loginUrl, accept, contentType, refer,
                language, postContent, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            System.Diagnostics.Debug.WriteLine(html);           
            return WangyiResover.Resove(html);
        }
        #endregion
    }
}
